package com.aaron.generic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liurong
 * @date 2021/9/8 13:19
 * @version:
 */
public class TestDemo {

    private static ThreadLocal<DateFormat> sdfLocal = new ThreadLocal<DateFormat>(){
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static void handleEquipmentOnlineStatusNotice(String str) {

        JSONObject jsonObject = (JSONObject) JSONObject.parse(str);

        //AEP平台设备ID
        String deviceId = (String) jsonObject.get("deviceId");
        //上线：1，下线：0
        String eventType = String.valueOf(jsonObject.get("eventType"));
        //时间戳
        Date date = new Date((Long) jsonObject.get("timestamp"));

        //将aep设备上下线数据转为心跳信息
        Map<String, Object> map = new HashMap<>();
        map.put("device_code", deviceId);
        String format = sdfLocal.get().format(date);
        map.put("device_report_time", format);
        map.put("platform_report_time", format);


        System.out.println(JSON.toJSONString(new HashMap<String, Object>() {
            {
                this.put("data", Arrays.asList(map));
            }
        }));


    }

    public static void main(String[] args) {

        handleEquipmentOnlineStatusNotice("{\n" +
                "    \"deviceId\":\"9df374dae2274942998bed25f6a451d6\",\n" +
                "    \"tenantId\":\"1\",\n" +
                "    \"productId\":\"10009151\",\n" +
                "    \"eventType\":1,\n" +
                "    \"messageType\":\"deviceOnlineOfflin eReport\",\n" +
                "    \"timestamp\":1554198201870\n" +
                "}");
        System.out.println(new Date(1554198201870L));

    }

}

